import {getCode, getCompanyList, getInfo, login, loginPhone} from '@/api/user'
import {getToken, removeToken, setToken} from '@/utils/auth'
import {resetRouter} from '@/router'

const state = {
  token: getToken(),
  name: '',
  avatar: '',
  userinfo: '',
  userdata: '',
  code:''
}

const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_USERINFO: (state, data) => {
    state.userinfo = data
  },
  SET_USERDATA: (state, data) => {
    state.userdata = data
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      login({ ...userInfo }).then(response => {
        const { data, headers } = response
        const token = `${data.oauth2AccessToken.token_type.replace(data.oauth2AccessToken.token_type[0],data.oauth2AccessToken.token_type[0].toUpperCase())} ${data.oauth2AccessToken.access_token}`
        commit('SET_TOKEN', token)
        commit('SET_NAME', data.user.username)
        commit('SET_AVATAR', data.user.icon)
        commit('SET_USERINFO', data.oauth2AccessToken)
        commit('SET_USERDATA', data.user)
        setToken(token)
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // user login
  loginPhone({ commit }, userInfo) {
    return new Promise((resolve, reject) => {
      loginPhone({ ...userInfo }).then(response => {
        const { data, headers } = response;
        const token = `${data.oauth2AccessToken.token_type.replace(data.oauth2AccessToken.token_type[0],data.oauth2AccessToken.token_type[0].toUpperCase())} ${data.oauth2AccessToken.access_token}`
        commit('SET_TOKEN', token)
        commit('SET_NAME', data.user.username)
        commit('SET_AVATAR', data.user.icon)
        commit('SET_USERINFO', data.oauth2AccessToken)
        commit('SET_USERDATA', data.user)
        setToken(token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get Code 获取验证码
  getCodeAct({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      getCode(data).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },
  getCompanyListAct({ commit, state }, data) {
    return new Promise((resolve, reject) => {
      getCompanyList(data).then(response => {
        const { data } = response
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      commit('SET_TOKEN', '')
      removeToken()
      resetRouter()
      resolve()
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

